<!--
Author: W3layouts
Author URL: http://w3layouts.com
License: Creative Commons Attribution 3.0 Unported
License URL: http://creativecommons.org/licenses/by/3.0/
-->
<!DOCTYPE html>
<html>
<head>
<title>Big store a Ecommerce Online Shopping Category Flat Bootstrap Responsive Website Template | Login :: w3layouts</title>
<!-- for-mobile-apps -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta property="og:title" content="Vide" />
<meta name="keywords" content="Big store Responsive web template, Bootstrap Web Templates, Flat Web Templates, Android Compatible web template, 
Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyEricsson, Motorola web design" />
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false);
function hideURLbar(){ window.scrollTo(0,1); } </script>
<!-- //for-mobile-apps -->
<link href="../css/bootstrap.css" rel='stylesheet' type='text/css' />
<!-- Custom Theme files -->
<link href="../css/style.css" rel='stylesheet' type='text/css' />
<link href="../css/font-awesome.css" rel="stylesheet">
<!-- js -->
<script src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../js/move-top.js"></script>
<script type="text/javascript" src="../js/easing.js"></script>
<script type="text/javascript">
	jQuery(document).ready(function($) {
		$(".scroll").click(function(event){		
			event.preventDefault();
			$('html,body').animate({scrollTop:$(this.hash).offset().top},1000);
		});
	});
</script>
<!-- start-smoth-scrolling -->



</head>
<body>
<div class="header">
	<div class="container">
		<div class="logo">
			<h1 ><a ><b>T<br>H<br>E</b>Big Store<span>The Best Supermarket</span></a></h1>
		</div>

		<script type="text/javascript">
            $(function(){
                var _this1=null;
                $('.nav>li').hover(function(){
                    _this1=$(this);
                    _this1.find('.second-nav').show();
                    var _this2=null;
                    _this1.find('.second-nav').find('li').hover(function(){
                        _this2=$(this);
                        _this2.find('.third-nav').show();
                        _this2.find('.third-nav').hover(function(){
                            $(this).show();
                        },function(){
                            $(this).hide();
                        });
                    },function(){
                        _this2.find('.third-nav').hide();
                    });
                },function(){
                    _this1.find('.second-nav').hide();
                });
            });
		</script>
		<div class="nav-top">
			<nav class="navbar navbar-default">
				<div class="collapse navbar-collapse" id="bs-megadropdown-tabs">
					<ul class="nav navbar-nav ">
						<li><a id="header_leibie" class="hyper "><span> 类别管理 </span></a></li>
						<li><a id="header_shangpin" class="hyper "><span> 商品管理 </span></a></li>
					</ul>
				</div>
			</nav>

			<div class="clearfix"></div>
		</div>

	</div>
</div>
  <!---->
 <!--banner-->
<div class="banner-top">
	<div class="container">
		<h3 >categories</h3>
		<h4><a >Home</a><label>/</label>categories</h4>
		<div class="clearfix"> </div>
	</div>
</div>
<!--login-->

	<div class="login" style="border: 1px solid red;padding: 10px 100px 0 100px">
		<div>
			<span style="font-size: 30px">类别列表</span>
			<div class="btn btn-info" id="btn" style=" float:right;margin-right:50px">
				<a onclick="addRootCategory()" style="color:white;">添加根类别</a>
			</div>
			<table class="table table-bordered" id="categoryRows">
				<tr>
					<th>ID</th><th>类名</th><th>描述</th><th>父ID</th><th>叶子节点</th><th>级别</th><th>操作</th>
				</tr>

			</table>
		</div>

	</div>
<script>
    //从浏览器URL获取GET请求参数
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
	var token = getQueryString('t')==null? '':getQueryString('t');
    //alert(typeof token);
	function addRootCategory() {
		window.location='addCategory.html?pid=0&t='+token;
    }
    function deleteCategory(id) {
	    if(confirm('确定删除吗？')){
            var responseBody = $.ajax({
                url:"/v1/categories/"+id,
                method:"DELETE",
                async:false,
            });
            if(responseBody.status==200 || responseBody.status==203 ){
                window.location='categories.html?t='+token;
            }
		}

    }
    function updateCategory(id) {
		window.location='addCategory.html?id='+id+'&t='+token;
    }
    function addLeafCategory(id) {
		window.location='addCategory.html?pid='+id+'&t='+token;
    }
    function loadCategories() {
	    document.getElementById('header_leibie').setAttribute('href','categories.html?t='+token);
        document.getElementById('header_shangpin').setAttribute('href','products.html?t='+token);
        var responseBody = $.ajax({
            url:"/v1/categories",
            method:"GET",
            async:false,
			headers:{t:token}
        });
        if(responseBody.status == 444){
            window.location='login.html';
		}else if(responseBody.status==200){
            var categoryRows = document.getElementById('categoryRows');
            var categoryData = responseBody.responseJSON;
            for(i=0;i<categoryData.length;i++){
                var tr = document.createElement('tr');
                var td_id = document.createElement('td');
                td_id.innerHTML=categoryData[i].id;
                var td_name = document.createElement('td');
                if(categoryData[i].grade==2){
                    td_name.innerHTML = '----'+categoryData[i].name;
				}else{
                    td_name.innerHTML = categoryData[i].name;
				}
				var td_desc = document.createElement('td');
				td_desc.innerHTML=categoryData[i].descr;
                var td_pid = document.createElement('td');
                td_pid.innerHTML=categoryData[i].pid;
                var td_leaf = document.createElement('td');
                td_leaf.innerHTML=categoryData[i].leaf;
                var td_grade = document.createElement('td');
                td_grade.innerHTML=categoryData[i].grade;
                var btn_delete = document.createElement('a');
                btn_delete.innerHTML='删除';
                btn_delete.setAttribute('class','btn btn-sm btn-danger');
                btn_delete.setAttribute('onclick','deleteCategory('+categoryData[i].id+')');
                var btn_update = document.createElement('a');
                btn_update.innerHTML='更改';
                btn_update.setAttribute('class','btn btn-sm btn-success');
                btn_update.setAttribute('onclick','updateCategory('+categoryData[i].id+')');
                var btn_addleaf = document.createElement('a');
                btn_addleaf.innerHTML='添加子类别';
                btn_addleaf.setAttribute('class','btn btn-sm btn-info');
                btn_addleaf.setAttribute('onclick','addLeafCategory('+categoryData[i].id+')');
                var td_operate = document.createElement('td');
                td_operate.appendChild(btn_delete);
                td_operate.appendChild(btn_update);
                if(categoryData[i].grade != 2){
                    td_operate.appendChild(btn_addleaf);
				}


                tr.appendChild(td_id);
                tr.appendChild(td_name);
                tr.appendChild(td_desc);
                tr.appendChild(td_pid);
                tr.appendChild(td_leaf);
                tr.appendChild(td_grade);
                tr.appendChild(td_operate);

                categoryRows.appendChild(tr);
			}
		}
    }
</script>


	<script type="text/javascript">
		$(document).ready(function() {
            loadCategories();
		/*
			var defaults = {
			containerID: 'toTop', // fading element id
			containerHoverID: 'toTopHover', // fading element hover id
			scrollSpeed: 1200,
			easingType: 'linear' 
			};
		*/								
		$().UItoTop({ easingType: 'easeOutQuart' });
		});
	</script>
	<a href="#" id="toTop" style="display: block;"> <span id="toTopHover" style="opacity: 1;"> </span></a>
<!-- //smooth scrolling -->
<!-- for bootstrap working -->
		<script src="../js/bootstrap.js"></script>
<!-- //for bootstrap working -->


</body>
</html>